// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;

namespace SnapObjects.Data
{
    /// <summary>
    ///     Provides a series of methods and properties for getting the ISqlBuilder object or SQL statement according 
    ///     to the definition in a model class.
    /// </summary>
    public class ModelSqlBuilder
    {
        private readonly DataContext _context;
        private readonly Type _modelType;

        private ModelSqlBuilder(DataContext context, Type modelType)
        {
            _context = context;
            _modelType = modelType;
        }

        /// <summary>
        ///     Creates a ModelSqlBuilder object with the specified model class and data context.
        /// </summary>
        /// <typeparam name="TModel">The type of the model class used to create the ISqlBuilder object.</typeparam>
        /// <param name="context">The DataContext object used to generate SQL statement.</param>
        /// <returns>Returns the newly created ModelSqlBuilder object.</returns>
        public static ModelSqlBuilder GetBuilder<TModel>(DataContext context)
        {
            return new ModelSqlBuilder(context, typeof(TModel));
        }

        /// <summary>
        ///     Gets a newly created ISqlQueryBuilder object based on the definition in the model class.
        /// </summary>
        public ISqlQueryBuilder QueryBuilder
            => new ModelSelectBuilder(_context).GetModelQueryBuilder(_modelType)
                                                .SqlQueryBuilder;

        /// <summary>
        ///     Gets a newly created ISqlQueryBuilder object based on the definition in the model class and the specified select list.
        /// </summary>
        /// <param name="selectName">The name of the select list predefined in the model class.</param>
        /// <returns>Returns the newly created ISqlQueryBuilder object.</returns>
        public ISqlQueryBuilder GetQueryBuilder(string selectName = null)
        {
            return new ModelSelectBuilder(_context).GetModelQueryBuilder(_modelType, selectName)
                                                           .SqlQueryBuilder;
        }

        /// <summary>
        ///     Gets a SQL SELECT statement based on the definition in the model class and the data context.
        /// </summary>
        public string QuerySql => this.QueryBuilder.ToSqlString(_context);

        /// <summary>
        ///     Gets the SQL SELECT statement based on the definition in the model class, the specified select list, and the data context.
        /// </summary>
        /// <param name="selectName">The name of the select list predefined in the model class.</param>
        /// <returns>Returns the newly generated SQL SELECT statement.</returns>
        public string GetQuerySql(string selectName)
        {
            return this.GetQueryBuilder(selectName).ToSqlString(_context);
        }

        /// <summary>
        ///     Gets a newly created ISqlDeleteBuilder object based on the definition in the model class.
        /// </summary>
        public ISqlDeleteBuilder DeleteBuilder
            => new ModelDeleteBuilder(_context, _modelType).GetDeleteBuilder();

        /// <summary>
        ///     Gets a SQL DELETE statement based on the definition in the model class and the data context.
        /// </summary>
        public string DeleteSql => this.DeleteBuilder.ToSqlString(_context);
    }
}
